package com.cgt.day8;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExceptionDemo {
    @Test
    public void test1() {
        String str = "abc";
        System.out.println(str.charAt(1));
        //System.out.println(str.charAt(3));
        //Exception exception = new ArithmeticException();
        System.out.println(3 / 0);
        /*try {
            System.out.println(3 / 0);
        } catch (Exception e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }*/

        //编译时异常：未雨绸缪
        try {
            //需要检查的代码（可能会抛出异常，也可能不会抛出异常）
            FileInputStream fileInputStream = new FileInputStream("io.txt");
        } catch (FileNotFoundException e) {
            //捕获异常后处理异常
            //throw new RuntimeException(e);
            e.printStackTrace();
        } finally {
            //一定会执行的代码，不管抛不抛出异常
            System.out.println("finally");
        }

        System.out.println("ExceptionDemo.test1");
    }

    @Test
    public void test34() {
        //加载JDBC驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }
        System.out.println("ExceptionDemo.test34");
    }

    @Test
    public void test35() {
        try {
            show();
        } catch (ClassNotFoundException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }
    }

    //抛出编译时异常，谁调用谁去处理
    public void show() throws ClassNotFoundException {
        //throw new ClassNotFoundException();
        Class.forName("com.mysql.jdbc.Driver");
        //FileInputStream fileInputStream = new FileInputStream("io.txt");
    }

    //IO: Input Output
    @Test
    public void test341() {
        try {
            //文件输入流
            FileInputStream fileInputStream = new FileInputStream("io.txt");
            fileInputStream.read();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally");
        }
    }

    @Test
    public void test343() {
        try {
            //文件输入流
            FileInputStream fileInputStream = new FileInputStream("io.txt");
            fileInputStream.read();
        } catch (IOException e) {
            e.printStackTrace();
        } /*catch (FileNotFoundException e) {
            //Exception 'java.io.FileNotFoundException' has already been caught
            e.printStackTrace();
        }*/ finally {
            System.out.println("finally");
        }
    }

    @Test
    public void test55() {
        try {
            quQian(2000);
        } catch (MeiQianException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }

        System.out.println("ExceptionDemo.test55");
    }

    //withdraw
    public void quQian(double money) throws MeiQianException {
        if (money >= 1000) {
            throw new MeiQianException("钱不够了");
        }

        System.out.println("钱够了");
    }
}

